import EventManager from "../Managers/EventManager";
import { EventTypes } from "../Managers/EventTypes";
import SDK from "./SDK";

export class TTSDK extends SDK {

    protected onEvents() {
        super.onEvents();
        EventManager.on(EventTypes.GameEvents.GameRun, this.onGameRun, this);
        EventManager.on(EventTypes.GameEvents.GameOver, this.onGameOver, this);
        //
        EventManager.on(EventTypes.SDKEvents.StartRecord, this.onStartRecord, this);
        EventManager.on(EventTypes.SDKEvents.PauseRecord, this.onPauseRecord, this);
        EventManager.on(EventTypes.SDKEvents.ResumeRecord, this.onResumeRecord, this);
        EventManager.on(EventTypes.SDKEvents.StopRecord, this.onStopRecord, this);
        EventManager.on(EventTypes.SDKEvents.ShareRecord, this.onShareRecord, this);
    }

    protected setAdCfg(): void {
        //-ttdc57aaea4369855202 -人类别逃 lsnULcqC612868       
        this.adConfig.adBannerIdList = ["107hg30d5864819bb2"];      // 指定 Banner 单元广告ID
        this.adConfig.adVideoIdList = ["r4ls0o9w1eln2cq7dj"];       // 指定激励视频广告ID,
        this.adConfig.adInterstitialId = "9e68432jgfd13ojab0";      // 指定插屏广告ID

        //--tt2d7e5bc3b240469602 -无双割草 
        // this.adConfig.adBannerIdList = ["1s03pea878o5b27kgf"];      // 指定 Banner 单元广告ID
        // this.adConfig.adVideoIdList = ["6jn85dfdomq896459a"];       // 指定激励视频广告ID,
        // this.adConfig.adInterstitialId = "d328b2kfki2bj04hb8";      // 指定插屏广告ID
        // this.adConfig.adCustomIdList = [""];                     // 指定原生模板广告ID，原生模板广告支持多个
        this.adConfig.shareInfoArr = [{ title: "一起来玩啊!" }]; // 自定义分享

        this.realNameCheck();
    }

    private realNameCheck() {
        console.log("TT 实名认证");
        if (window['tt']) {
            window['tt'].onRealNameAuthenticationComplete(function (obj) {
                console.log("实名认证完成回调 ", obj.state);
            });
        }
    }

    //#region -------------功能------

    //插屏
    protected showInterstitial() {
        console.log('TT 关闭插屏');
    }
    /**
     * 拉起激励视频广告 --无广告 test
     */
    // protected showRewardedVideo(cb: Function | { success: Function, fail: Function, cancel: Function }) {
    //     let success = null;
    //     let fail = null;
    //     let cancel = null;
    //     if (typeof cb === 'object') {
    //         success = cb.success;
    //         fail = cb.fail;
    //         cancel = cb.cancel;
    //     } else {
    //         success = cb;
    //     }

    //     success && success();
    //     //暂停游戏
    //     // EventManager.emit(EventTypes.GameEvents.GamePause);
    //     // AudioSystem.pauseBGM();
    //     // //
    //     // console.warn('观看视频中');
    //     // uniSdk.showRewardedVideo(0, (status: number) => {
    //     //     if (status == 1) {
    //     //         console.log("激励视频已观看完毕 ");
    //     //         success && success();
    //     //         //恢复游戏
    //     //         EventManager.emit(EventTypes.GameEvents.GameResume);
    //     //         AudioSystem.resumeBGM();
    //     //     }
    //     //     else if (status == 0) {
    //     //         console.log("激励视频未观看完毕 ");
    //     //         cancel && cancel();
    //     //         EventManager.emit(EventTypes.GameEvents.GameResume);
    //     //         AudioSystem.resumeBGM();
    //     //     }
    //     //     else {
    //     //         console.log("激励视频加载失败 ");
    //     //         fail && fail();
    //     //         EventManager.emit(EventTypes.GameEvents.GameResume);
    //     //         AudioSystem.resumeBGM();
    //     //     }

    //     // }, this);
    // }

    //#endregion

    //#region --------事件---------
    protected onGameRun() {
        this.onStartRecord();
    }

    protected onGameOver() {
        this.onStopRecord();
    }

    //--录屏
    protected onStartRecord() {
        uniSdk.AdPlat.instance.startRecord();
    }
    protected onPauseRecord() {
        uniSdk.AdPlat.instance.pauseRecord();
    }
    protected onResumeRecord() {
        uniSdk.AdPlat.instance.resumeRecord();
    }
    //停止录屏（头条）
    protected onStopRecord() {
        uniSdk.AdPlat.instance.stopRecord();
    }
    //分享录屏（头条）
    protected onShareRecord(success: Function, fail: Function = null) {
        uniSdk.AdPlat.instance.share({ channel: 'video' }, (isSuccess) => {
            if (isSuccess) {
                success && success();
            } else {
                fail && fail();
            }
        }, () => {
            this.showMessage('录屏时间过短');
        }
        );
    }

    //#endregion
}
/**录屏数据记录 */
class RecordVideoData {
    public startTime: number;
    public endTime: number;
    public gameShareCount: number;
    public dayShareCount: number;
    public constructor(data) {
        this.startTime = 0;
        this.endTime = 0;
        this.gameShareCount = 0;
        if (!!data) {
            this.dayShareCount = data.dayShareCount;
        } else {
            this.dayShareCount = 0;
        }
    }
    /**录屏开始 */
    public onStart() {
        this.startTime = Date.now();
        this.endTime = Date.now();
    }
    /**录屏结束 */
    public onEnd() {
        this.endTime = Date.now();
    }
    /**录屏时长，单位：秒 */
    public get totalTime() {
        return (this.endTime - this.startTime) * 0.001;
    }
    /**分享成功 */
    public onShare() {
        this.gameShareCount++;
        this.dayShareCount++;
    }
}